//
//  HBBaseRefreshController.m
//  HeartBeat
//
//  Created by karong on 2023/8/23.
//

#import "HBBaseRefreshController.h"

@interface HBBaseRefreshController ()
@property (nonatomic, strong) MJRefreshAutoNormalFooter *mj_footer;
@end

@implementation HBBaseRefreshController
@synthesize dataArray;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.mj_footer.triggerAutomaticallyRefreshPercent = 0;
    self.mj_footer.refreshingTitleHidden = YES;
    self.mj_footer.stateLabel.textColor = UIColor.grayColor;
    self.mj_footer.stateLabel.font = [UIFont systemFontOfSize:12];
    [self.mj_footer setTitle:WSLocalized(@"没有更多內容了")forState:MJRefreshStateNoMoreData];
    self.listView.mj_footer = self.mj_footer;
    self.listView.mj_footer.hidden = YES;
}

- (void)mjStyleDidChange {
    self.mj_footer.stateLabel.textColor = UIColor.bg_gray_f2f2f2;
    if (@available(iOS 12.0, *)) {
        if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
            self.mj_footer.loadingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        }
        else {
            self.mj_footer.loadingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        }
    }
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
}

- (void)benginRefresh {
    [self.listView.mj_header beginRefreshing];
}

- (void)refreshData {
    WeakSelf;
    weakSelf.emptyState = HBListViewEmptyStateEmpty;
    [weakSelf.request refreshDataSuccess:^(NSArray *array) {
        [weakSelf loadSuccessWithArray:array isRefresh:YES];
    } failed:^(NSURLResponse *response, NSError *error, id responseObject) {
        [weakSelf loadFailedWithError:error];
    }];
}

- (void)loadMoreData {
    WeakSelf;
    [weakSelf.request loadMoreDataSuccess:^(NSArray *array) {
        [weakSelf loadSuccessWithArray:array isRefresh:NO];
    } failed:^(NSURLResponse *response, NSError *error, id responseObject) {
        [weakSelf loadFailedWithError:error];
    }];
}

- (void)loadSuccessWithArray:(NSArray *)array isRefresh:(BOOL)isRefresh {
    WeakSelf;
    if ([weakSelf.listView respondsToSelector:@selector(reloadData)]) {
        if (isRefresh) {
            [weakSelf.dataArray removeAllObjects];
        }
        [weakSelf.dataArray addObjectsFromArray:array];
        id obj = weakSelf.listView;
        [obj reloadData];
        [[obj collectionViewLayout] invalidateLayout];
        [obj layoutIfNeeded];
        [weakSelf.listView.mj_header endRefreshing];
        [weakSelf.listView.mj_footer endRefreshing];
        [weakSelf loadCompleteWithArray:array];
    }
}

- (void)loadCompleteWithArray:(NSArray *)array {
    WeakSelf;
    weakSelf.mj_footer.hidden = array.count <= 0;
    if(weakSelf.request.haveMore) {
        [weakSelf.mj_footer resetNoMoreData];
        weakSelf.mj_footer.stateLabel.hidden = YES;
    }
    else {
        [weakSelf.mj_footer endRefreshingWithNoMoreData];
        weakSelf.mj_footer.stateLabel.hidden = NO;
    }
}

- (void)loadFailedWithError:(NSError *)error {
    WeakSelf;
    [weakSelf.listView.mj_header endRefreshing];
    [weakSelf.listView.mj_footer endRefreshing];
    // 隐藏当前的上拉刷新控件
    weakSelf.listView.mj_footer.hidden = !weakSelf.request.haveMore || weakSelf.dataArray.count == 0;
    if (error) {
        [weakSelf analysisError:error];
    }
    else {
        weakSelf.emptyState = HBListViewEmptyStateNoToken;
    }
    id obj = weakSelf.listView;
    [obj reloadData];
}

// 解析失敗、無顯示网络
- (void)analysisError:(NSError *)error {
    WeakSelf;
    weakSelf.emptyState = HBListViewEmptyStateNoNetwork;
    if (error.code == NSURLErrorNotConnectedToInternet || error.code == NSURLErrorCannotDecodeContentData) {
        /// 网络异常
        [HBToast simple:WSLocalized(@"网络异常")];
    }
    else if (error.code == NSURLErrorBadServerResponse || (error.code > 1000)) {
        /// 服务器無响应
        [HBToast simple:WSLocalized(@"服务器無响应")];
    }
    else {
        /// 其他問題
        [HBToast simple:error.localizedDescription];
    }
}


- (MJRefreshAutoNormalFooter *)refreshFooter {
    return self.mj_footer;
}

@end
